<?php
// +----------------------------------------------------------------------
// | BIX [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016  All rights reserved.
// +----------------------------------------------------------------------
// | Author: @wangyejing
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

/**
 * 购物车控制器
 * @author  Mr wang
 */
class CartController extends HomeController
{
	/* 购物车页面 */
	public function showcart()
	 {
		$goodsinfo = D('Cart');
		$data = $goodsinfo->goodsinfo();
		$this->assign('data',$data);
		$this->display();
	}

	//ajax更新购物车点击事件
	public function  modifycart(){
		$modify = D('cart');
		$data = $modify->updatacart();
		echo $data;
	}


	/* 结算页 */
	public function account()
	{
		$res 			= I('post.');
		$session  = I('session.');

		// 获取user_id
		$user_id  = session()['user_id'];

		/*查询地址并分配数据*/
		$addr     = D('Cart');
		$addres   = $addr->select_addr();
		$this->assign('addres',$addres);

		/*查询购物车表并分配数据*/
		$cart     = D('cart')->where("user_id = $user_id and status=0")->select();
		foreach($cart as &$v){
			$cart['sub_num']   += $v['num'];
			$cart['sub_total'] += $v['num'] * $v['price']; 
		}
		$this->assign('cart',$cart);

		/*  查询积分表并分配数据  */
		$interger = $addr->intergerinfo();
		$this->assign('inter',$interger);

		//查询支付表并分配数据
		$data     = $addr->show();
		$this->assign('data',$data);
		$this->display();

	}

	/*  订单确认页 */
	public function vieworder()
	{
		if(IS_GET){
			$order_num = I('get.order_num');
			if(null   != $order_num){
				$info    = D('order')
					->alias('a')
					->join("north_order_detail b ON a.order_num=b.order_num")
					->join("north_user_addr c ON a.addr_id=c.addr_id")
					->join("north_details d ON b.details_id=d.details_id")
					->join("north_goods e ON b.goods_id=e.goods_id")
					->join("north_image f ON b.goods_id=f.goods_id")
					->where("a.order_num='{$order_num}'")
					->find();

				$order = [];
				$order['addrinfo'][0]['receiver'] = $info['receiver'];
				$order['addrinfo'][0]['province'] = $info['province'];
				$order['addrinfo'][0]['city'] = $info['city'];
				$order['addrinfo'][0]['phone'] = $info['phone'];
				$order['remark'] = $info['remark'];
				$order['cartinfo'][0]['goods_name'] = $info['goods_name'];
				$order['cartinfo'][0]['type'] = $info['number'];
				$order['cartinfo'][0]['color'] = $info['color'];
				$order['cartinfo'][0]['size'] = $info['size'];
				$order['cartinfo'][0]['num'] = $info['good_num'];
				$order['cartinfo'][0]['price'] = $info['price'];
				$order['cartinfo'][0]['image'] = "Uploads".ltrim($info['path'],'.').$info['name'];
				$order['k'] = $info['good_num'];
				$order['t'] = $info['good_num'] * $info['price'];
				$order['order_num'][0] = $info['order_num'];
			}else{
				$this->error('页面不存在',U('index/index'));
			}
		}else{
				//实例化并分配数据
				$data  = D('cart');

				// vieworder页面禁止重复刷新，判断方法：当查出的购物车数据为空时
				$uid 						= I('post.')['user_id'];
				$map['user_id'] = ['eq',$uid];
				$map['status']  = ['eq',0];
				$judge_cart = $data->where($map)->select();
				if(null 				== $judge_cart){
					$this->error('您已提交订单，请到个人中心的订单列表付款！',U('personCenter/personCenter'));
					exit;
				}

				// 否则将数据插入订单表和订单详情表
				$order = $data->addorder();
		}

		// 将要修改订单状态的订单号，存入session 中，键名为'ordre_num'
		session('order_num',$order['order_num']);

		$this->assign('addrinfo',$order['addrinfo']);
		$this->assign('cartinfo',$order['cartinfo']);
		$this->assign('remark',$order['remark']);
		$this->assign('userid' , $order['userid']);
		$this->assign('jifen', $order['jifen']);
		$this->assign('payinfo',$order['payinfo']);
		$this->assign('k',$order['k']);
		$this->assign('t',$order['t']);

		$this->display();
	}


	/*  付款页 */
	public function payfor()
	{
		/* 判断是否登录*/
		if(null == session()['user_id']){
			$this->error('您还没有登录，请先登录！',U('User/login'));
		}elseif(null == I('post.')){
			$this->error('页面不存在',U('index/index'));
		}

		$info      = I('post.');
		$pay_order_num  = session()['order_num'];

		$payforin = D('cart');
		if(null != $info['jifen']){
			// 修改积分
			$res1     = $payforin ->modifyinteger();
		}
		
		// 修改订单状态为已付款
		$res2       = $payforin->modifyorder($pay_order_num); 
		$this->assign('sole',$info['sole']);
		$this->display();
	}


	//ajax删除购物车
	public function delcart($id){
		$delin = D('cart');
		$data = $delin->delcart();
		echo $data;
	}

	//ajax添加地址信息
	public function addrinfo(){
		$addrin = D('cart');
		$res = $addrin->addrinfo();
		echo $res;
	}
}
